package com.czy.owner.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.czy.owner.utils.PhoneUtils;

/**
 * Created by asus on 2017/12/14.
 */

public class DividerGridItemDecoration extends RecyclerView.ItemDecoration {

    private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
    private Drawable mDivider;
    private int lineWidth;
    private int rightWidth;

    public DividerGridItemDecoration(Context context) {
        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = new ColorDrawable(Color.parseColor("#ffffff"));
        lineWidth = PhoneUtils.dip2px(context, 12f);
        rightWidth = PhoneUtils.dip2px(context, 6f);
        a.recycle();
    }

    public DividerGridItemDecoration(int color) {
        mDivider = new ColorDrawable(color);
    }

    public DividerGridItemDecoration() {
        this(Color.parseColor("#cccccc"));
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        drawHorizontal(c, parent);

    }



    public void drawHorizontal(Canvas c, RecyclerView parent) {
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int left = child.getLeft() - params.leftMargin;
            final int right = child.getRight() + params.rightMargin
                    + lineWidth;
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }




    private boolean isFirstRaw(RecyclerView parent, int pos, int spanCount,
                               int childCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {


            if (pos % spanCount == 0)// 如果是第一行，绘制左侧
                return true;
        }

        return false;
    }


    @Override
    public void getItemOffsets(Rect outRect, int itemPosition,
                               RecyclerView parent) {
        int spanCount = 3;
        int childCount = parent.getAdapter().getItemCount() - 1;
        if (itemPosition > 0) {

            if (isFirstRaw(parent, itemPosition - 1, spanCount, childCount)) {// 如果是第一，绘制左侧
                outRect.set(rightWidth, 0, lineWidth, 0);
            } else {
                outRect.set(0, 0,lineWidth , 0);
            }
        }

    }
}
